var name,
	tel,
	province,
	city,
	area,
	address,
	maxaddress,
	priceall = 0,
	pricetotal = 0;

(function () {
    $.ajax({
        url: 'user.php?act=getstatusaddress',
        dataType: 'json',
        type: 'post',
        success: function (e) {
			// console.log(e)
            $('.input_name').val(e.name)
            $('.input_tel').val(e.tel)
			$('.shouhuo span').html(e.province + e.city + e.area + e.address)
			$('#distpicker_change').distpicker({
				province: e.province,
				city: e.city,
				district: e.area,
			});
			$('.address-detail').val(e.address)
			ghuoqus(e)
        }
	})
	
	$.ajax({
		url:'user.php?act=order_list&ajax=true',
		dataType: 'json',
		type: 'post',
		data:{
			pagesize:100
		},
		success:function(e){
			// console.log(e.list)
			var obj = e.list
			for(var i=0;i<e.list.length;i++){
				// price.prictlist.push(obj[i])
				// priceall = parseFloat(priceall) + parseFloat(obj[i].price)
				// console.log(parseFloat(obj[i].price))
				// console.log(priceall)
			}
			// price.qian = '￥' + priceall
			// console.log(priceall)
		}
	})
}())

function suanqian(){
	for(var i = 0;i<goodslist.length;i++){
		pricetotal += parseFloat(goodslist[i].goods.price)*parseInt(goodslist[i].goods_num)
	}
	price.qian = '￥' + pricetotal
	console.log(pricetotal)
}


var vms = new Vue({
	el:'#mask',
	data:{
		masklist:[]
	},
	methods: {
		maskhandle:function(idx){
			name = this.masklist[idx].name
			tel = this.masklist[idx].tel
			province = this.masklist[idx].province
			city = this.masklist[idx].city
			area = this.masklist[idx].area
			address = this.masklist[idx].address
			ins()
			$('.mask, .macon').removeClass('show');
		}
	}
})
console.log(goodslist)
var price = new Vue({
	el:'#shopcart',
	data:{
		prictlist:goodslist,
		flag:false,
		qian:''
	},
	methods:{
		add2Order:function(){
			console.log($('.shouhuo span')[0].innerHTML)
			console.log(pricetotal)
			maxaddress = $('.shouhuo span')[0].innerHTML
			const data = new URLSearchParams
			console.log(this.prictlist)
			const goods = this.prictlist.map(e =>({
				iscomment:0,
				id: e.goods_id,
				price: e.goods.price,
				num: e.goods_num,
				name: e.goods.title
			}))
			console.log(goods)
			const address = {
				name: name,
				tel: tel,
				address: maxaddress
			}
			data.append('goods', JSON.stringify(goods))
			data.append('address', JSON.stringify(address))
			data.append('total', pricetotal)
			data.append('remarks', 'I am remarks')
			axios.post('user.php?act=add2order&ajax=true', data).then(r => {
				console.log(r)
				if (r.data.code == 1) {
					console.log(this.carts)
					this.$message({
					  message: '操作成功',
					  type: 'success'
					})
					// this.getAllData()
					window.location.href = 'user.php?act=order_success&order_id='+r.data.order_id
				  } else {
					// alert('操作失败')
					this.$message.error('兄弟，醒醒，没钱还想下单？')
				  }
			})
			
		}
	}
})

suanqian()

// $('#distpicker').css({'display':'none'})

$('.changes').click(function () {
	console.log($('.input_name'))
	$('.input_name')[0].disabled = false
	$('.input_tel')[0].disabled = false
	$('#distpicker_change').distpicker({
		province: province,
		city: city,
		district: area,
	});
	$('#distpicker_change').css({'display':'flex'}),
	$('.shouhuo span').css({'display':'none'})
	$('.ok').css({'display':'block'})
})



$('.ok').click(function(){
	ok()
})

$('.right #add').on('click', function () {
	// $('#distpicker').css({'display':'block'})
	ok()
	console.log($('.shen'))
	$('.input_name').val(name)
	$('.input_tel').val(tel)
	$('#add').css({'display':'none'})
	$('#distpicker').distpicker({
		province: province,
		city: city,
		district: area,
	})
	$('.address-detail').val(address)
})

$('#submit').click(function(){
	ghuoqu()
	$.ajax({
		url: 'user.php?act=update_or_insert_address&ajax=true',
		dataType: 'json',
		type: 'post',
		data:{
			name:name,
			tel:tel,
			province:province,
			city:city,
			area:area,
			address:address
		},
		success: function (e) {
			alert('保存成功')
		}
	})
})

$('.change').click(function(){
	$.ajax({
		url:'user.php?act=getaddress',
		dataType: 'json',
		type: 'post',
		success:function(e){
			var obj = e
			for(var i=0;i<e.length;i++){
				vms.masklist.push(obj[i])
			}
		}
	})
})

$('.cha').click(function(){
	vms.masklist = []
})

function ok(){
	ghuoqu()
	$('.input_name').val(name)
	$('.input_tel').val(tel)
	$('.shouhuo span').html(province + city + area + address)
	$('#distpicker_change').distpicker({
		province: province,
		city: city,
		district: area,
	});
	$('.address-detail').val(address)

	$('.input_name')[0].disabled = true
	$('.input_tel')[0].disabled = true
	$('#distpicker_change').css({'display':'none'}),
	$('.shouhuo span').css({'display':'block'})
	$('.ok').css({'display':'none'})
}

function ins() {
	$('.input_name').val(name)
	$('.input_tel').val(tel)
	$('.shouhuo span').html(province + city + area + address)
}

function ghuoqu() {
	name = $('.input_name').val()
	tel = $('.input_tel').val()
	province = $('.shen').val()
	city = $('.shi').val()
	area = $('.qu').val()
	address = $('.address-detail').val()
}

function ghuoqus(e) {
	name = $('.input_name').val()
	tel = $('.input_tel').val()
	address = $('.address-detail').val()
	province = e.province
	city = e.city
	area = e.area
}
